/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyphenate.easeui.ui;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.model.EaseImageCache;
import com.hyphenate.easeui.utils.EaseImageUtils;
import com.hyphenate.easeui.utils.EaseLoadLocalBigImgTask;
import com.hyphenate.easeui.widget.photoview.EasePhotoView;
import com.hyphenate.easeui.widget.photoview.PhotoViewAttacher.OnPhotoTapListener;
import com.hyphenate.util.EMLog;
import com.hyphenate.util.ImageUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * download and show original image
 * 
 */
public class EaseShowBigImageActivity extends EaseBaseActivity {
	private static final String TAG = "ShowBigImage";
	private ProgressDialog pd;
	private EasePhotoView image;
	private int default_res = R.mipmap.ease_default_image;
	private String localFilePath;
	private Bitmap bitmap;
	private boolean isDownloaded;
	private ProgressBar loadLocalPb;
	private String urlString;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
		setContentView(R.layout.ease_activity_show_big_image);
		super.onCreate(savedInstanceState);

		image = (EasePhotoView) findViewById(R.id.image);
		loadLocalPb = (ProgressBar) findViewById(R.id.pb_load_local);
		default_res = getIntent().getIntExtra("default_image",
				R.mipmap.ease_default_avatar);
		Uri uri = getIntent().getParcelableExtra("uri");
		String remotepath = getIntent().getExtras().getString("remotepath");
		localFilePath = getIntent().getExtras().getString("localUrl");
		String secret = getIntent().getExtras().getString("secret");
		EMLog.d(TAG, "show big image uri:" + uri + " remotepath:" + remotepath);

		// show the image if it exist in local path
		if (uri != null && new File(uri.getPath()).exists()) {
			EMLog.d(TAG, "showbigimage file exists. directly show it");
			urlString = uri.getPath();
			DisplayMetrics metrics = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(metrics);
			// int screenWidth = metrics.widthPixels;
			// int screenHeight =metrics.heightPixels;
			bitmap = EaseImageCache.getInstance().get(uri.getPath());
			if (bitmap == null) {
				EaseLoadLocalBigImgTask task = new EaseLoadLocalBigImgTask(
						this, uri.getPath(), image, loadLocalPb,
						ImageUtils.SCALE_IMAGE_WIDTH,
						ImageUtils.SCALE_IMAGE_HEIGHT);
				if (android.os.Build.VERSION.SDK_INT > 10) {
					task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
				} else {
					task.execute();
				}
			} else {
				image.setImageBitmap(bitmap);
			}
		} else if (remotepath != null) { // download image from server
			EMLog.d(TAG, "download remote image");
			Map<String, String> maps = new HashMap<String, String>();
			if (!TextUtils.isEmpty(secret)) {
				maps.put("share-secret", secret);
			}
			downloadImage(remotepath, maps);
		} else {
			image.setImageResource(default_res);
		}
		
		creatSavePop();

		image.setOnPhotoTapListener(new OnPhotoTapListener(){

			@Override
			public void onPhotoTap(View view, float x, float y) {
				finish();
			}});
		
		image.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				mPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, 0, 0);
				return false;
			}
		});
	}

	/**
	 * download image
	 * 
	 * @param remoteFilePath
	 */
	@SuppressLint("NewApi")
	private void downloadImage(final String remoteFilePath,
			final Map<String, String> headers) {
		String str1 = getResources().getString(R.string.Download_the_pictures);
		pd = new ProgressDialog(this);
		pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pd.setCanceledOnTouchOutside(false);
		pd.setMessage(str1);
		pd.show();
		File temp = new File(localFilePath);
		final String tempPath = temp.getParent() + "/temp_" + temp.getName();
		final EMCallBack callback = new EMCallBack() {
			public void onSuccess() {

				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						new File(tempPath).renameTo(new File(localFilePath));

						DisplayMetrics metrics = new DisplayMetrics();
						getWindowManager().getDefaultDisplay().getMetrics(
								metrics);
						int screenWidth = metrics.widthPixels;
						int screenHeight = metrics.heightPixels;

						bitmap = ImageUtils.decodeScaleImage(localFilePath,
								screenWidth, screenHeight);
						if (bitmap == null) {
							image.setImageResource(default_res);
						} else {
							image.setImageBitmap(bitmap);
							EaseImageCache.getInstance().put(localFilePath,
									bitmap);
							isDownloaded = true;
						}
						if (EaseShowBigImageActivity.this.isFinishing()
								|| EaseShowBigImageActivity.this.isDestroyed()) {
							return;
						}
						if (pd != null) {
							pd.dismiss();
						}
					}
				});
			}

			public void onError(int error, String msg) {
				EMLog.e(TAG, "offline file transfer error:" + msg);
				File file = new File(tempPath);
				if (file.exists() && file.isFile()) {
					file.delete();
				}
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						if (EaseShowBigImageActivity.this.isFinishing()
								|| EaseShowBigImageActivity.this.isDestroyed()) {
							return;
						}
						image.setImageResource(default_res);
						pd.dismiss();
					}
				});
			}

			public void onProgress(final int progress, String status) {
				EMLog.d(TAG, "Progress: " + progress);
				final String str2 = getResources().getString(
						R.string.Download_the_pictures_new);
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						if (EaseShowBigImageActivity.this.isFinishing()
								|| EaseShowBigImageActivity.this.isDestroyed()) {
							return;
						}
						pd.setMessage(str2 + progress + "%");
					}
				});
			}
		};

		EMClient.getInstance().chatManager()
				.downloadFile(remoteFilePath, tempPath, headers, callback);

	}

	@Override
	public void onBackPressed() {
		if (isDownloaded)
			setResult(RESULT_OK);
		finish();
	}
	
	
	
	private PopupWindow mPopupWindow;
	private void creatSavePop() {
		if (mPopupWindow == null) {
			View view = getLayoutInflater().inflate(R.layout.sex_popupwindows, null);
			mPopupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT,true);
			mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
			view.findViewById(R.id.ll_xj).setVisibility(View.GONE);
			Button item_popupwindows_Photo = (Button) view.findViewById(R.id.item_popupwindows_Photo);
			item_popupwindows_Photo.setText("保存图片");
			item_popupwindows_Photo.setOnClickListener(new OnClickListener()
	        {

				public void onClick(View v)
	            {
					
					mPopupWindow.dismiss();
					String fileName = EaseImageUtils.getFileNameFromUrl(urlString);
					//文件夹是否存在 不存在要穿件
					File file2 = new File(EaseConstant.DIR_IMAGE_SAVE);
					if (!file2.exists()) {
						file2.mkdirs();
					}
					
					File file = new File(EaseConstant.DIR_IMAGE_SAVE, fileName);
					if (file.exists()) {
						Toast.makeText(getApplicationContext(), "图片已保存到"+EaseConstant.DIR_IMAGE_SAVE, Toast.LENGTH_SHORT).show();
					}else{
						Bitmap bitmap= ((BitmapDrawable)image.getDrawable()).getBitmap();
						EaseImageUtils.getFileFromBitmap(bitmap, file);
						Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
						Uri uri = Uri.fromFile(file);
						intent.setData(uri);
						sendBroadcast(intent);
						Toast.makeText(getApplicationContext(), "图片已保存到"+EaseConstant.DIR_IMAGE_SAVE, Toast.LENGTH_SHORT).show();
					}
	            }
	        });
	        view.findViewById(R.id.item_popupwindows_cancel).setOnClickListener(new OnClickListener()
	        {
	            public void onClick(View v)
	            {
	                mPopupWindow.dismiss();
	            }
	        });
		}
	}
}
